package com.highcom.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.highcom.pojo.Company;
import com.highcom.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @RequestMapping("/danweiguanli")
    public ModelAndView findAllDepts(@RequestParam(defaultValue = "1",required = true,value = "pageNo") Integer pageNo){
        ModelAndView m = new ModelAndView();
        //开始分页
        PageHelper.startPage(pageNo,3);
        List<Company> deptsList = companyService.findCompanys();
        //把要分页的集合数据添加到PageInfo中
        PageInfo<Company> pageInfo = new PageInfo<>(deptsList);
        m.addObject("companyInfo", pageInfo);
        m.setViewName("/files/danweiguanli.jsp");
        return m;
    }

    @RequestMapping("/delCompanyById")
    public String delCompanyById(int id) {
        System.out.println(id);
        companyService.delCompanyById(id);
        return "redirect:/danweiguanli";
    }

    @RequestMapping("/delCompanysByids")
    public String delCompanysByids(int[] ids){
        System.out.println(ids);
        companyService.delCompanysByIds(ids);
        return "redirect:/danweiguanli";
    }

    @RequestMapping("/findCompanyById")
    public ModelAndView findCompanyById(int id) {
        ModelAndView m = new ModelAndView();
        Company companyById = companyService.findCompanyById(id);
        m.addObject("companyInfo", companyById);
        m.setViewName("/files/danweimodify.jsp");
        return m;
    }

    @RequestMapping("/modifyCompany")
    public String modifyRole(Company company) {
        System.out.println(company.getId());
        companyService.modifyCompany(company);
        return "redirect:/danweiguanli";
    }

    @RequestMapping("/toaddCompany")
    public String toaddCompany() {
        return "/files/danweiadd.jsp";
    }

    @RequestMapping("/addCompany")
    public String addRole(Company company) {
        companyService.addCompany(company);
        return "redirect:/danweiguanli";
    }

    @RequestMapping("/queryCompany")
    public ModelAndView findRolesLikeQuery(Company company,@RequestParam(defaultValue = "1",required = true,value = "pageNo") Integer pageNo) {
        System.out.println(company.toString());
        ModelAndView m = new ModelAndView();
        //开始分页
        PageHelper.startPage(pageNo,3);
        List<Company> rolesLikeQuery = companyService.findCompanysLikeQuery(company);
        //把要分页的集合数据添加到PageInfo中
        PageInfo<Company> pageInfo = new PageInfo<>(rolesLikeQuery);
        m.addObject("companyInfo", pageInfo);
        m.setViewName("/files/danweiguanli.jsp");
        return m;
    }
}
